package com.project.movie.controller.brand;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.movie.common.enums.ResultCode;
import com.project.movie.domain.MovieBrand;
import com.project.movie.domain.MovieCinema;
import com.project.movie.domain.Result;
import com.project.movie.service.MovieBrandService;
import com.project.movie.service.MovieCinemaService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 超级管理员
 * @version 1.0
 * @description: 品牌controller
 * @date 2025/02/06 11:05
 */
@Controller
@ResponseBody
@RequestMapping("brand")
public class MovieBrandController {

    @Autowired
    private MovieBrandService movieBrandService;
    @Autowired
    private MovieCinemaService movieCinemaService;

    /** 分页获取品牌 */
    @PostMapping("getMovieBrandPage")
    public Result getMovieBrandPage(@RequestBody MovieBrand movieBrand) {
        Page<MovieBrand> page = new Page<>(movieBrand.getPageNumber(),movieBrand.getPageSize());
        QueryWrapper<MovieBrand> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .like(StringUtils.isNotBlank(movieBrand.getName()),MovieBrand::getName,movieBrand.getName());
        Page<MovieBrand> movieBrandPage = movieBrandService.page(page, queryWrapper);
        return Result.success(movieBrandPage);
    }

    @GetMapping("getMovieBrandList")
    public Result getMovieBrandList() {
        List<MovieBrand> brandList = movieBrandService.list();
        return Result.success(brandList);
    }

    /** 根据id获取品牌 */
    @GetMapping("getMovieBrandById")
    public Result getMovieBrandById(@RequestParam("id")String id) {
        MovieBrand movieBrand = movieBrandService.getById(id);
        return Result.success(movieBrand);
    }

    /** 保存品牌 */
    @PostMapping("saveMovieBrand")
    public Result saveMovieBrand(@RequestBody MovieBrand movieBrand) {
        boolean save = movieBrandService.save(movieBrand);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 编辑品牌 */
    @PostMapping("editMovieBrand")
    public Result editMovieBrand(@RequestBody MovieBrand movieBrand) {
        boolean save = movieBrandService.updateById(movieBrand);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 删除品牌 */
    @GetMapping("removeMovieBrand")
    public Result removeMovieBrand(@RequestParam("ids")String ids) {
        if (StringUtils.isNotBlank(ids)) {
            String[] asList = ids.split(",");
            for (String id : asList) {
                MovieBrand brand = movieBrandService.getById(id);
                QueryWrapper<MovieCinema> queryWrapper = new QueryWrapper<>();
                queryWrapper.lambda().eq(MovieCinema::getBrand,brand.getName());
                int count = movieCinemaService.count(queryWrapper);
                if (count > 0) {
                    return Result.fail("品牌下存在影院无法删除");
                }
                movieBrandService.removeById(id);
            }
            return Result.success();
        } else {
            return Result.fail("品牌id不能为空！");
        }
    }

}
